import {
  _decorator,
  Component,
  Node,
  Sprite,
  UITransform,
  Animation,
  AnimationClip,
  animation,
  Vec3,
  SpriteFrame,
  debug,
} from 'cc'
import { TILE_HEIGHT, TILE_WIDTH } from '../Scripts/Tile/TileManage'
import ResourceManage from '../Runtime/resourceManage'
import {
  CONTROLLER_ENUM,
  DIRECTION_ENUM,
  DIRECTION_ORDER_ENUM,
  ENTITY_STATE_ENUM,
  ENTITY_TYPE_ENUM,
  EVENT_ENUM,
  PARAMS_NAME_ENUM,
} from '../Enums'
import EventManage from '../Runtime/eventManage'
import { DoorStateMachine } from './DoorStateMachine'
import { EntityManage } from '../Base/EntityManage'
import DataManage from '../Runtime/dataManage'
import { IEntity } from '../Levels'
const { ccclass, property } = _decorator

@ccclass('DoorManage')
export class DoorManage extends EntityManage {
  async init(params: IEntity) {
    // 初始化人物动画状态机
    this.fsm = this.addComponent(DoorStateMachine)
    await this.fsm.init()
    super.init(params)
    // 监听玩家移动，改变敌人朝向
    EventManage.Instance.on(EVENT_ENUM.DOOR_OPEN, this.onOpen, this)
  }
  onDestroy() {
    super.onDestroy()
    EventManage.Instance.off(EVENT_ENUM.DOOR_OPEN, this.onOpen)
  }
  onOpen() {
    // 如果每个敌人都死亡，则打开门
    if (DataManage.Instance.enemies.every(enemy => enemy.state === ENTITY_STATE_ENUM.DEATH)) {
      this.state = ENTITY_STATE_ENUM.DEATH
    }
  }
}
